<?php

namespace app\queue;

use app\event\FacebookPixelTrackEvent;
use app\event\PushOrderToShopify;
use app\exception\PushShopifyOrderException;
use app\exception\QueueRuntimeException;
use app\helpers\RedisLock;
use app\model\LineItems;
use app\model\Notify;
use app\model\Orders;
use app\service\shopify\action\graph\OrderGraph;
use app\trait\ShopifyTrait;
use think\facade\Event;
use think\queue\Job;

class CompleteDraftQueue
{
    use ShopifyTrait;
    public function fire(Job $job, $data)
    {
        try{
            $order = Orders::findOrEmpty($data);
            $this->completedDraftOrder($order,true);
            //上报FB
            Event::trigger("FacebookPixelTrackEvent",new FacebookPixelTrackEvent($order));
            $job->delete();
        }catch (QueueRuntimeException $e){
            $job->delete();
        } catch (PushShopifyOrderException $e) {
            if($job->attempts() <= 2){
                $job->release(5);
            }else{
                $job->delete();
            }
        }
    }

    public function failed($data,$e)
    {
        $order = Orders::findOrEmpty($data);
        if(!$order->isEmpty()) Orders::saveExceptionMessage($order,$e->getMessage());
    }

}